const INFO_KEY = 'shopping_info_key'
const HISTORY_KEY = 'search_history'
export const getInfo = () => {
  const defaultObj = { token: '', userId: -1 }
  const result = localStorage.getItem(INFO_KEY)
  return result ? JSON.parse(result) : defaultObj
}

export const setInfo = (obj) => {
  localStorage.setItem(INFO_KEY, JSON.stringify(obj))
}

export const removeInfo = () => {
  localStorage.removeItem(INFO_KEY)
}

export const getHistoryList = () => {
  const result = localStorage.getItem(HISTORY_KEY)
  // 检查是否为 null 或者字符串 "undefined"
  if (result === null || result === 'undefined') {
    return []
  }
  return JSON.parse(result)
}

export const setHistoryList = (array) => {
  localStorage.setItem(HISTORY_KEY, JSON.stringify(array))
}
